<?php

namespace App\Admin\Controllers;

use Encore\Admin\Layout\Content;
use Carbon\Carbon;
use App\Models\MaterialImage;
use App\Models\Material;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Controllers\AdminController;

class MaterialImagesController extends AdminController
{
    protected $title = '素材图列表';

    protected function material($id = null)
    {
        $materialId = $id ?? request('material_id');
        if (!$material = Material::find($materialId)) {
            return back()->with(admin_error('必须依附于当前素材'));
        }
        return $material;
    }
    
    protected function grid()
    {
        $grid = new Grid(new MaterialImage());
	    
        $grid->model()->where('material_id', request('material_id'))->orderByDesc('id');

	    $grid->disableExport();
	    
	    $grid->column('id', 'ID');
        $grid->column('image', '图片');
        $this->gridActions($grid);
      
        return $grid;
    }
    
    protected function gridActions(Grid $grid)
    {
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
        });
    }


    protected function detail($id)
    {
        $show = new Show(MaterialImage::findOrFail($id)); 

        return $show;
    }

    public function edit($id, Content $content)
    {   

        return $content
            ->title($this->title())
            ->description($this->description['edit'] ?? trans('admin.edit'))
            ->body($this->form($id)->edit($id));
    }

    
    protected function form($id = null)
    {
        $form = new Form(new Material());
        $material = $this->material($id);
        $form->multipleImage('images', '图片')->removable()->sortable();

        

        // $this->formSaved($form);
        $this->formTools($form,$material);
        $this->formFooter($form);
         $this->formSaved($form, $material);

        $form->setAction("/admin/material-images/{$id}?material_id={$id}");
        
        return $form;
    }

   protected function formTools(Form $form, Material $material)
    {
        $form->tools(function (Form\Tools $tools) use ($material) {
            $tools->disableList();
            $tools->disableView();
            $tools->disableDelete();
            $tools->prepend('
                <a
                    href="'.route('admin.materials.index').'"
                    class="btn btn-sm btn-default"
                    style="margin-right: 5px">
                    <i class="fa fa-reply"></i> 列表
                </a>
            ');
        });
    }

    protected function formSaved(Form $form, Material $material)
    {
        $form->saved(function () use ($material) {
            return redirect(route('admin.materials.index'));
        });
    }
    protected function formFooter(Form $form)
    {
        $form->footer(function (Form\Footer $footer) {
            $footer->disableViewCheck();
            $footer->disableCreatingCheck();
            $footer->disableEditingCheck();
        });
    }
  
  
}
